package com.hh.generator.core.generator;

import com.alibaba.fastjson.JSON;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.Map;

public abstract class AbstractGenerator {

    protected String doGenerate(Object metadata, String templateName) {
        Map<String, Object> map = JSON.parseObject(JSON.toJSONString(metadata));
        VelocityContext context = new VelocityContext(map);
        //渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = Velocity.getTemplate("template/" + templateName, "UTF-8");
        tpl.merge(context, sw);
        return sw.toString();
    }

}
